<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>



<script type="text/javascript">

	function f(variavel){
		alert(variavel);
	}
	
	
	function innerFunction(param) {
		function b(theinput) {
			return theinput*2;
		};
		return 'The result is ' + b(param);
	}
	
	
	function innerExample() {
		alert(innerFunction(10));
	}
	
	
	// self-invoking function because it is called when it is assigned to var (double bracket in the end)
	var a = function() {
		
		// this function is going to be executed
		function someSetup() {
			var setup = 'done';
		}
		
		function actualWork() {
			alert('work-worky');
		}
		
		someSetup();
		
		// var 'a' will going to have the code of this function (no parentheses needed)
		return actualWork;
	}();

	
	
	// explains the scope (the environment, or world that the function executes in) 
	function scope() {
		
		// create object
		var person = {
			name:	'maria',
			age:	'18'
		};
		
		// add another field
		person.address = 'somewhere';
			
		var sayHello = function(person) {
			return "hello, i am " + person.name + " and i am " + person.age + " years old.";
		};
		
		// add a function to a var 
		person.doSomething = function() {
			// 'this' is the data in person obj
			return "hello, i am " + this.name + " and i am " + this.age + " years old.";
		};
		
		alert(person.doSomething());
	}
	
	
	
	
	var zaphod = {name: 'Zaphod', age: 42};
	var marvin = {name: 'Marvin', age: 30};

	zaphod.sayHello = function() {
		return "hi, i am " + this.name;
	}
	
	// ... yes the text to print from the call 'marvin.sayHello' is 'hi, i am Marvin'
	marvin.sayHello = zaphod.sayHello;
	
	
	
	
	

 	
	// make calculations 
	function calc1() {
		
		// create two functions
		var plus 	= function(x,y) {return x + y;}
		var minus	= function(x,y) {return x - y;}
		
		// associate the operations to the functions
		var operations = {
			'+':	plus,
			'-':	minus
		};

		// make the calculation
		var calculate = function(x,y,op) {
			return operations[op](x,y);
		}
		
		// show result
		alert(calculate(48,38,'+'));	
	}
	
	
	
	
	function calc2() {
		
		// this will work like a class
		var Problem = function(x,y) {
			this.x = x;
			this.y = y;
		};
	
		Problem.prototype.operations = {
			'+':	function(x,y) {return x + y;},
			'-':	function(x,y) {return x - y;}
		};
		
		Problem.prototype.calculate = function(op) {
			return this.operations[op](this.x + this.y);
		};
	
		// the 'new' keyword to tell the interpreter that we are using 'Problem'
		// not as a function but as a constructor ... if it is not present the function will execute.
		// the 'new' keyword gives us a copy of the function after running it
		var problem1 = new Problem(4, 5);
		
		// make the calculations
		var addCalc = problem1.calculate('+');
		var subCalc = problem1.calculate('-');
		console.log(addCalc);
		console.log(subCalc);
	}
	
	
function tmp() {
	var boat = {
		size: 		'normal',
		boatInfo: 	function() {
			alert(this === boat);
			alert(this.size);
		}
	};
}	
	

</script>
</head>
<body>
<form>
<input type="button" value="innerFunction" onclick="innerExample()" />
<input type="button" value="scope" onclick="scope()" />
<input type="button" value="calc1" onclick="calc1()" />
<input type="button" value="calc2" onclick="calc2()" />
</form>
</body>
</html>